<?php

/** 
 * 设备_控制器
 * @property User_dev_mdl $userDev
 * @property Dev_mdl $dev
 * @property School_mdl $school
 */
class Dev extends MY_Controller {
	
	public function __construct() {
		parent::__construct ();
		$this->user = parent::getLoginUser();
		$this->load->model('User_dev_mdl','userDev');
		$this->load->model('Dev_mdl','dev');
		$this->load->model('Tester_mdl','tester');
		$this->load->model('School_mdl','school');
	}
	/**
	 * 用户链接过的设备列表
	 */
	public function index(){
		
		parent::checkBindPhone($this->user);
		
		$select = 'id,name,mac,alias,createTime,usedCount';
		$list  = $this->userDev->getDevListByUid($this->user->id,$select);
		 
		foreach($list as $k=>$v){
			if($v['name']==$v['alias']){
				$if_have_alias=$this->userDev->getDevListByTest($v['id']);
				if($if_have_alias){
					$list[$k]['alias']=$if_have_alias['alias'];
				}
			}
		}
		$this->apiOutput->put('list', $list)->out();
	}
	/**
	 * 首次链接成功后纪录该设备
	 * 传入参数:
	 * 	mac:设备MAC
	 * 	name:设备名
	 *  alias:别名
	 */
	public function add(){
		$input = &$_REQUEST;
		if( empty($input['name']) ){
			$this->apiOutput->errorInput();
		}
		$name  = trim($input['name']);
		$mac   = empty($input['mac']) ? NULL : trim($input['mac']) ;
		$alias = empty($input['alias']) ?  NULL : $input['alias'] ;
		// 检查是否有记录过该设备(查dev表)
		$where = array();
		$where['name'] = $name;
		$dev = $this->dev->getOne($where);
		if( empty($dev) ){// 如果没有纪录过,说明设备名未被重命名
			$this->apiOutput->error('设备未初始化,请等待工作人员初始化设备',-2);
		}else{
			$devID = $dev['id'];
			 if( $mac && empty($dev['mac']) ){//如果之前没有传过mac,更新到dev表
				$update = array('mac'=>$mac);
				$where  = array('id'=>$dev['id']);
				$this->dev->update($where,$update);
			}
		}
		// 检查该用户有没有链接过该设备(查user_dev表)
		$where = array();
		$where['userID'] = $this->user->id;
		$where['devID']  = $devID;
		$recode = $this->userDev->getOne($where);
		if (empty($recode)) {// 如果没有链接过,纪录该设备
			$alias = empty($alias) ? $name : $alias;
			$recode = array();
			$recode['devID'] = $devID;
			$recode['userID']= $this->user->id;
			$recode['alias'] = $alias;
			$recode['createTime'] = time();
			$res = $this->userDev->insert($recode);
			if( !$res ){
				$this->apiOutput->errorSystem();
			}
		}else if ( $alias && $recode['alias'] !=$alias  ){//如果传入了别名且别名跟之前不一样,更新别名
			$this->_updateAlias($this->user->id, $recode['devID'], $alias);
		}else{
			$alias = $recode['alias'];
		}
		
		$dev['id'] = (int)$devID;
		$dev['alias'] = $alias;
		
		$this->apiOutput->put('dev', $dev)->out();
	}
	/**
	 * API:更新设备信息
	 * 传入参数: 
	 * 		devID:设备ID
	 * 		alias:设备别名
	 */
	public function update(){
		$devID = (int)$this->input->post_get('devID',TRUE);
		$alias = $this->input->post_get('alias',TRUE);
		if( empty($devID) || empty($alias) ){
			$this->apiOutput->errorInput();
		}
		$res = $this->_updateAlias($this->user->id, $devID, $alias);
		if ($res){
			$this->apiOutput->msg('修改成功!');
		}else{
			$this->apiOutput->errorSystem();
		}
	}
	/**
	 * 修改设备别名(同步更新订单表中的别名)
	 * @param int $userID
	 * @param int $devID
	 * @param string $alias
	 * @return bool
	 */
	private function _updateAlias($userID,$devID,$alias){
		$where = array();
		$where['userID'] = $userID;
		$where['devID']  = $devID;
		$update = array();
		$update['alias'] = $alias;
		
		$res = $this->userDev->update($where, $update);
// 		if( $res ){
// 			$this->load->model('Order_mdl','order');
// 			$this->order->update($where, $update);
// 		}
		return $res;
	}
	/**
	 * 初始化设备
	 * 传入参数:
	 * 	name:设备名
	 * 	mac
	 */
	public function init(){
		
		// 检查是否为测试人员
		if ( $this->user->isTest != 1 ) {
			$this->apiOutput->error('无操作权限',-1);
		}
		
		$where = array();
		$where['userID'] = $this->user->id;
		// 检查该测试账号是否已经失效
		$tester = $this->tester->getOne($where);
		if( empty($tester) || time() > $tester['invalidTime'] ){
			$this->apiOutput->error('调试账号已失效',-2);
		}
		
		// 检测输入
		$name = $this->input->post_get('name',TRUE);
		$mac = $this->input->post_get('mac',TRUE);
		$type = (int)$this->input->post_get('type');
		$schoolID = (int) $this->input->post_get('schoolID');
		$name = trim($name);
		$mac  = trim($mac);
		if( empty($name) || $schoolID <=0 ){
			$this->apiOutput->errorInput();
		}
		// 查找是否存在该名字的设备
		$where = array();
		$where['name'] = $name;
		$dev = $this->dev->getOne($where);
		
		if ( ! empty($dev) ) {
		    $this->apiOutput->error('该设备已初始化',-11);
		}
		// 如果不存在,生成新名称并插入
		$maxID = $this->school->incrementDevID($schoolID);// 获取设备在该校的最大ID
		if ( ! $maxID ) {
		    $this->apiOutput->errorSystem();
		}
		// 生成设备名
// 		$type   = $type === 2 ? 2 : 1;//默认为"饮水机"
		$checkType = array(1,2,3);
		$type = in_array($type, $checkType)?$type:1;
		
		$prefix = 'RJ';//默认洗衣机模式
		
		switch ( $type ){
			case 1:
				$prefix = 'RJ';
				break;
			case 2:
				$prefix = 'RX';
				break;
			case 3:
				$prefix = 'RL';
				break;
			default:
			    $prefix = 'RJ';
		}
		$name = sprintf($prefix."%04s%04s",$schoolID,$maxID);
		
		$dev = array();
		$dev['name']        = $name;
		$dev['mac']         = $mac;
		$dev['type']        = $type;
		$dev['schoolID']    = $schoolID;
		$dev['schoolDevID'] = $maxID;
		$dev['createTime']  = time();
		$devID = $this->dev->insert($dev);
		if ( ! $devID ) {
			$this->apiOutput->errorSystem();
		}
		$this->apiOutput->put('status', 1)->put('name', $name)->out();
	}
	
	/**
	 * 设备报错
	 */
	public function error_log(){
		
		$devID = $this->input->post('devID',true);
		$user_id = $this->user->id;
		
		// 检查该设备是否可用
		$dev = $this->dev->getOneById($devID);
		
		if( empty($dev) || $dev['enable']==0 ){
			$this->apiOutput->error('该设备不可用',-101);
		}
		
		$this->load->model('Dev_error_log_mdl','dev_error_log');
		
		$data = array(
				'userID'=>$user_id,
				'dev_id'=>$dev['id'],
				'dev_name'=>$dev['name'],
				'dev_type'=>$dev['type'],
				'create_time'=>time()
				);
		
		//记录设备日志
		$in = $this->dev_error_log->insert($data);
		
		if ($in) {
			//将设备禁用
			$up = $this->dev->update(array('id'=>$devID), array('enable'=>0));
		}
		
		$this->apiOutput->msg('提交成功!');
		
	}
	
	
	
}

